package repo_sqlx

import (
	"fmt"

	"github.com/jmoiron/sqlx"

	"clean/usecase"
)


func createInitTables(db *sqlx.DB) (e error) {
	defer func() {
		err := recover()
		if err != nil {
			e = fmt.Errorf("%v", err)
		}
	}()
	createHello(db)
	return
}

func createHello(db *sqlx.DB) {
	createSql := `CREATE TABLE IF NOT EXISTS "hello" (
	"name" VARCHAR(255) NOT NULL,
	"msg" VARCHAR(255) NOT NULL,
	primary key (name)
);`
	db.MustExec(createSql)
}

func InitRepo(db *sqlx.DB) (*usecase.Repo, error) {
	err := createInitTables(db)
	if err != nil {
		return nil, err
	}
	return &usecase.Repo{
		HelloRepo: NewHelloRepo(db),
	}, nil
}
